package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ApplicationContext {
	
	//给一个文件名传入进来
	private String fileName;

	public ApplicationContext() {
		fileName = "applicationContext.xml";
	}

	public ApplicationContext(String fileName) {
		super();
		this.fileName = fileName;
	}

	//调用getBean，根据id返回对象
	public Object getBean(String id) {
		
		//1.判断文件是否存在，不存在返回null
		File f = new File(fileName);
		if(!f.exists()) {
			return null;
		}
		
		//2.解析配置文件
		SAXReader reader = new SAXReader();
		//3.读取了配置文件，返回了一个Document对象
		Document dom = null;
		try {
			dom = reader.read(new FileInputStream(fileName));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (DocumentException e1) {
			e1.printStackTrace();
		}
		//4.得到此Dom树的根元素
		Element root = dom.getRootElement();
		//5.得到所有二级bean元素们
		List<Element> beans = root.elements();
		
		for (Element bean : beans) {
			//得到class整个属性
			Attribute classAttribute = bean.attribute("class");
			//得到id整个属性
			Attribute idAttribute = bean.attribute("id");
			//如果id属性值等于参数id
			if(idAttribute.getValue().equals(id)) {
				//得到class的属性值
				String className = classAttribute.getValue();
				
				//得到实际的类类型
				Class classType = null;
				try {
					classType = Class.forName(className);
				} catch (ClassNotFoundException e1) {
					e1.printStackTrace();
				}
				//创建此类对象
				Object obj = null;
				try {
					obj = classType.newInstance();
				} catch (InstantiationException e1) {
					e1.printStackTrace();
				} catch (IllegalAccessException e1) {
					e1.printStackTrace();
				}
				
				//得到这个bean的所有子元素
				List<Element> properties = bean.elements();
				//遍历所有的属性元素
				for (Element property : properties) {
					String methodName = "set" + Character.toUpperCase(property.attributeValue("name").charAt(0)) 
										+ property.attributeValue("name").substring(1); //age-> setAge
					String methodValue = property.attributeValue("value");
					
					//得到方法的参数类型，即属性类型
					try {
						Field field = classType.getDeclaredField(property.attributeValue("name"));
						//得到此属性的类型
						Class<?> type = field.getType();
						//根据属性不同强制类型转换成对应属性
						Method method = classType.getMethod(methodName,type);
						//调用
						if(type.getName().equals("int")) {
							method.invoke(obj,Integer.parseInt(methodValue));
						}else if(type.getName().equals("float")) {
							method.invoke(obj,Float.parseFloat(methodValue));
						}else if(type.getName().equals("double")) {
							method.invoke(obj,Double.parseDouble(methodValue));
						}else if(type.getName().equals("char")) {
							method.invoke(obj,methodValue.charAt(0));
						}else if(type.getName().equals("boolean")) {
							method.invoke(obj,Boolean.parseBoolean(methodValue));
						}else {
							method.invoke(obj,methodValue);
						}
					} catch (NumberFormatException e) {
						e.printStackTrace();
					} catch (NoSuchFieldException e) {
						e.printStackTrace();
					} catch (SecurityException e) {
						e.printStackTrace();
					} catch (NoSuchMethodException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						e.printStackTrace();
					}
					
				}
				return obj;
				
			}
		}
		
		return null;
	}

}
